package ioc

import (
	"fmt"
	"github.com/IBM/sarama"
	"github.com/spf13/viper"
	"webook/feed/events"
)

func InitKafka() sarama.Client {
	type Config struct {
		Addrs []string `yaml:"addrs"`
	}

	saramaCfg := sarama.NewConfig()
	saramaCfg.Producer.Return.Successes = true

	var cfg Config
	err := viper.UnmarshalKey("kafka", &cfg)
	if err != nil {
		panic(fmt.Errorf("读取kafka配置失败 %w", err))
	}

	client, err := sarama.NewClient(cfg.Addrs, saramaCfg)
	if err != nil {
		panic(fmt.Errorf("创建sarama客户端失败 %w", err))
	}
	return client
}

func NewConsumers(article *events.ArticleEventConsumer, feed *events.FeedEventConsumer) []events.Consumer {
	return []events.Consumer{
		article,
		feed,
	}
}
